//
//  InputView.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/8/2.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "InputView.h"

@interface InputView()<UITextViewDelegate>

@property(nonatomic,strong)UILabel *placeLabel; //模拟 placeholder
@property(nonatomic,strong)UITextView *textView;

@end

@implementation InputView
{
    NSString *_placeText;
}

-(instancetype)initWithFrame:(CGRect)frame
                    delegate: (id)delegate
                 placeholder: (NSString*)placeText
               returnKeyType: (UIReturnKeyType)keyType{
    self = [super initWithFrame:frame];
    if (self) {
        _delegate = delegate;
        _placeText = placeText;
        
        UITextView *tv = [[UITextView alloc]initWithFrame: CGRectMake(20, 20, CGRectGetWidth(frame)-40, CGRectGetHeight(frame)-40)];
        tv.font = [UIFont systemFontOfSize:16];
        tv.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
        tv.delegate = self;
        tv.layer.borderColor = [UIColor grayColor].CGColor;
        tv.layer.borderWidth = 1;
        tv.layer.cornerRadius = 4;
        tv.backgroundColor = [UIColor lightTextColor];
        [tv addSubview: self.placeLabel];
        tv.keyboardType = UIKeyboardTypeDefault;
        tv.returnKeyType = keyType;
        
        _textView = tv;
        
        [self addSubview:_textView];
    }
    return self;
}


-(UILabel *)placeLabel{
    if (!_placeLabel) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 16)];
        label.text = _placeText;
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor lightGrayColor];
        label.userInteractionEnabled = false;
        _placeLabel = label;
    }
    return _placeLabel;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {// return key
        if ([_delegate respondsToSelector:@selector(inputViewDidClickedReturnButton)]) {
            [self.delegate inputViewDidClickedReturnButton];
        }
        [_textView endEditing:true];
        return false;
    }
    return true;
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    if ([_delegate respondsToSelector:@selector(inputViewDidEndInputingWithText:)]) {
        [self.delegate inputViewDidEndInputingWithText: [self getInputedText]];
    }
}

-(void)textViewDidChange:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        _placeLabel.text = _placeText;
    }
    else{
        _placeLabel.text = @"";
    }
}

-(NSString *)getInputedText{
    NSString *text = [_textView.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
    return text;
}


-(void)setInputedText:(NSString *)text{
    if (text && text.length > 0) {
        self.placeLabel.text = @"";
        _textView.text = text;
    }
}

-(void)setInputable:(BOOL)inputable{
    _textView.editable = inputable;
    _textView.layer.borderWidth = (inputable) ? 1 : 0;
}

@end
